package com.heima.search.service.impl;

import com.heima.common.dtos.ResponseResult;
import com.heima.search.dto.UserSearchDto;
import com.heima.search.pojo.AssociateWords;
import com.heima.search.service.ApAssociateWordsService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ApAssociateWordsServiceImpl implements ApAssociateWordsService {

    //这里在config里已经定义为一个bean，将其放在容器中，我们直接Autowired自动注入
    @Autowired
    public RestHighLevelClient restClient;

    /**
     * 联想词
     *
     * @param userSearchDto
     * @return
     */
    @Override
    public ResponseResult findAssociate(UserSearchDto userSearchDto) {

        //将搜索词取出来
        String searchWords = userSearchDto.getSearchWords();

        log.info("联想关键字{}", searchWords);

        //调用方法得到联想词集合
        List<AssociateWords> associativeWord = getAssociativeWord(searchWords);

        System.out.println(associativeWord);

        return ResponseResult.okResult(associativeWord);
    }


    /**
     * 获取联想词
     *
     * @return
     */
    public List<AssociateWords> getAssociativeWord(String searchWords) {

        List<CompletionSuggestion.Entry.Option> options = new ArrayList<>();

        //创建一个列表用来存放建议词信息
        //List<String> texts = new ArrayList<>();

        List<AssociateWords> arrayList = new ArrayList<>();

        try {
            //获取request请求
            SearchRequest request = new SearchRequest("lead_news");
            //DSL
            request.source().suggest(new SuggestBuilder().addSuggestion(
                    "name_suggest",
                    SuggestBuilders.completionSuggestion("suggestion")
                            .prefix(searchWords)
                            .skipDuplicates(true)
                            .size(15)));
            //发送请求
            SearchResponse response = restClient.search(request, RequestOptions.DEFAULT);

            //解析响应
            //最外层的suggest
            Suggest responseSuggest = response.getSuggest();
            //通过suggest名称得到补全结果
            //Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>>
            CompletionSuggestion suggestion = responseSuggest.getSuggestion("name_suggest");

            //获取options，就是包括所有联想词的集合
            options = suggestion.getOptions();

            for (CompletionSuggestion.Entry.Option option : options) {

                //新建联想词类
                AssociateWords associateWords = new AssociateWords();

                //获取到建议词信息
                Text text = option.getText();

                //将联想词放进去
                associateWords.setAssociateWords(text.toString());

                //将类放到集合中
                arrayList.add(associateWords);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        //将联想词集合返回
        return arrayList;

    }

}

